#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cctype>

using namespace std;

double _EPS = 2.2e-16;

#include "EasyBMP.h"
#include "EasyBMP_Geometry.h"
#include "EasyBMP_Font.h"

#include "tinyxml.h"
#include "xml_parser.h"
#include "Matrix.h"
#include "ImprovedMath.h"

int PictureCoordinate( int i, int EdgeLength )
{
 return i*(EdgeLength+1);
}

int main( int argc, char* argv[] )
{
 if( argc < 4 )
 {
  cout << "Correct usage: VesselPlot <x_connection_file> <y_connection_file> <output_file>" << endl
       << "         e.g.: VesselPlot XBR_5_00000000.dat YBR_5_00000000.dat vessels.bmp" << endl << endl;
  return -1;
 }
 
 BMP Temp; 
 int EdgeLength = 5; 
 
 Matrix XBR;
 XBR.ReadFromFile( argv[1] );
 Matrix YBR;
 YBR.ReadFromFile( argv[2] );
 
 Temp.SetSize( 1 + (YBR.TellRows()-1)*(EdgeLength+1) , 1 + (YBR.TellCols()-1)*(EdgeLength+1) );
 
 RGBApixel VesselColor;
 VesselColor.Red = 255;
 VesselColor.Green = 0;
 VesselColor.Blue = 0;
 
 RGBApixel WHITE;
 WHITE.Red = 255;
 WHITE.Green = 255;
 WHITE.Blue = 255;
 
 double min_vessel_radius = 2e-6;
 double max_vessel_radius = 1e-5;
 double rad_diff = max_vessel_radius - min_vessel_radius;
 
 for( int i=0 ; i < XBR.TellRows()-2 ; i++ )
 {
  for( int j=0; j < XBR.TellCols() ; j++ )
  {
   int I = i;
   int J = YBR.TellCols()-j-1;
   *Temp( PictureCoordinate(I,EdgeLength), PictureCoordinate(J,EdgeLength) ) = VesselColor;
   
   double intensity_x = ( *XBR(i,j) - min_vessel_radius )/rad_diff;
   if( intensity_x > 1 ){ intensity_x = 1.0; }
   if( intensity_x < 0 ){ intensity_x = 0.0; }
   
   double intensity_y = ( *YBR(i,j) - min_vessel_radius )/rad_diff;
   if( intensity_y > 1 ){ intensity_y = 1.0; }
   if( intensity_y < 0 ){ intensity_y = 0.0; }
   
   RGBApixel VesselColorX;
   
   VesselColorX.Red = (ebmpBYTE) ( (1.0-intensity_x)*WHITE.Red + intensity_x*VesselColor.Red );
   VesselColorX.Green = (ebmpBYTE) ( (1.0-intensity_x)*WHITE.Green + intensity_x*VesselColor.Green );
   VesselColorX.Blue = (ebmpBYTE) ( (1.0-intensity_x)*WHITE.Blue + intensity_x*VesselColor.Blue );
   
   RGBApixel VesselColorY;
   
   VesselColorY.Red = (ebmpBYTE) ( (1.0-intensity_y)*WHITE.Red + intensity_y*VesselColor.Red );
   VesselColorY.Green = (ebmpBYTE) ( (1.0-intensity_y)*WHITE.Green + intensity_y*VesselColor.Green );
   VesselColorY.Blue = (ebmpBYTE) ( (1.0-intensity_y)*WHITE.Blue + intensity_y*VesselColor.Blue );

   if( intensity_x > 1e-5 )
   {
    DrawFastLine( Temp, PictureCoordinate(I,EdgeLength)-1,PictureCoordinate(J,EdgeLength),
 	                    PictureCoordinate(I-1,EdgeLength)+1,PictureCoordinate(J,EdgeLength),
                  VesselColorX );
   }
   if( intensity_y > 1e-5 )
   {   
    DrawFastLine( Temp, PictureCoordinate(I,EdgeLength),PictureCoordinate(J,EdgeLength)-1,
	                    PictureCoordinate(I,EdgeLength),PictureCoordinate(J-1,EdgeLength)+1,
                  VesselColorY );
   }				  
 /*   
   if( *XBR(i,j) > 7e-6 && I > 0)
   {
    DrawFastLine( Temp, PictureCoordinate(I,EdgeLength),PictureCoordinate(J,EdgeLength),
	                    PictureCoordinate(I-1,EdgeLength),PictureCoordinate(J,EdgeLength),
                  VesselColor );
   }
   if( *YBR(i,j) > 7e-6 && J > 0)
   {
    DrawFastLine( Temp, PictureCoordinate(I,EdgeLength),PictureCoordinate(J,EdgeLength),
	                    PictureCoordinate(I,EdgeLength),PictureCoordinate(J-1,EdgeLength),
                  VesselColor );
				  
   }
*/   
  }
 }
 
 Temp.WriteToFile( argv[3] );


 return 0;
}
